"""
数据验证工具
提供输入数据验证功能
"""

import re
import logging
from typing import Dict, Any, List

logger = logging.getLogger(__name__)

class Validators:
    """数据验证工具类"""
    
    @staticmethod
    def validate_email(email: str) -> bool:
        """验证邮箱格式"""
        pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
        return bool(re.match(pattern, email))
    
    @staticmethod
    def validate_phone(phone: str) -> bool:
        """验证手机号格式"""
        # 中国手机号验证
        pattern = r'^1[3-9]\d{9}$'
        return bool(re.match(pattern, phone))
    
    @staticmethod
    def validate_username(username: str) -> bool:
        """验证用户名格式"""
        # 用户名长度3-20，只能包含字母、数字、下划线
        pattern = r'^[a-zA-Z0-9_]{3,20}$'
        return bool(re.match(pattern, username))
    
    @staticmethod
    def validate_password(password: str) -> bool:
        """验证密码强度"""
        # 密码长度至少6位
        if len(password) < 6:
            return False
        return True
    
    @staticmethod
    def validate_resume_data(data: Dict[str, Any]) -> List[str]:
        """验证简历数据"""
        errors = []
        
        # 检查必填字段
        required_fields = ['name', 'content']
        for field in required_fields:
            if not data.get(field):
                errors.append(f"{field} 是必填字段")
        
        # 验证邮箱
        if data.get('email') and not Validators.validate_email(data['email']):
            errors.append("邮箱格式不正确")
        
        # 验证手机号
        if data.get('phone') and not Validators.validate_phone(data['phone']):
            errors.append("手机号格式不正确")
        
        return errors
    
    @staticmethod
    def sanitize_text(text: str) -> str:
        """清理文本内容"""
        if not text:
            return ""
        
        # 移除多余的空白字符
        text = re.sub(r'\s+', ' ', text.strip())
        
        # 移除特殊字符（保留中文、英文、数字、常用标点）
        text = re.sub(r'[^\w\s\u4e00-\u9fff\-\.\,\!\?\(\)]', '', text)
        
        return text 